<template>
  <div>
    <div class="top">
      <div class="page-content topic-introduce">
        <p class="title mb-16">{{$t('topic_details')}}</p>
        <div class="detail mb-16">
          <t-image :src="obj.headImg" alt="" class="cover img-cover mr-16" />
          <div>
            <p class="name">{{ obj.specialName }}</p>
            <p class="num">{{ obj.contentCount }}{{$t('xxx_content')}}</p>
          </div>
        </div>
        <p class="summary">
          {{ obj.summary }}
        </p>
      </div>
    </div>
    <div class="page-content pt-24">
      <div class="layout">
        <div class="left mr-24">
          <friendly-scroll-list
            :is-show-loading="showLoading"
            :is-empty="!listData || !listData.length"
            :is-error="currentState.error"
            :is-no-more="currentState.noMore"
            :is-requesting="currentState.requesting"
            :more-load-action="getList"
            :scroll-load-action="loadMore"
            @refresh="getList(true)"
          >
            <news-item
              v-for="(item, index) in listData"
              :key="index"
              :obj="item"
              :show-tag="true"
            />
          </friendly-scroll-list>
        </div>
        <div class="right">
          <scroll-to-fixed>
            <hot-topic :list="hotsTopicList" />
          </scroll-to-fixed>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import FriendlyScrollList from '@/components/FriendlyScrollList.vue';
import NewsItem from '@/components/NewsItem.vue';
import HotTopic from '@/components/HotTopic.vue';
import ScrollToFixed from '@/components/ScrollToFixed.vue';

const COUNT = 15;

export default {
  components: {
    ScrollToFixed,
    FriendlyScrollList,
    NewsItem,
    HotTopic
  },

  async asyncData({ params, $API }) {
    const { id } = params;
    const [obj, contentRes, hotsTopicList] = await Promise.all([
      $API.subjectGetDetail({
        subjectId: id
      }),
      $API.subjectListContents({
        topicId: id,
        pageNum: 1,
        pageSize: COUNT
      }),
      $API.subjectListHots({
        subjectId: id
      })
    ]);
    const currentState = {
      id,
      requesting: false,
      isEmpty: false,
      error: false,
      noMore: false,
      pageNum: 2
    };

    const { list, pageNum = 0, pages = 0 } = contentRes;
    if (list.length === 0) {
      currentState.isEmpty = true;
    } else if ((list.length && list.length < COUNT) && (pages <= pageNum)) {
      currentState.noMore = true;
    }

    return {
      obj,
      currentState,
      listData: list || [],
      hotsTopicList: hotsTopicList || [],
      showLoading: false
    };
  },

  data() {
    return {
      obj: {},
      listData: [],
      hotsTopicList: [],
      showLoading: true,
      currentState: {
        id: null,
        requesting: false,
        isEmpty: false,
        error: false,
        noMore: false,
        pageNum: 1
      }
    };
  },

  methods: {
    async getList(isClear = false) {
      if (this.currentState.requesting || this.currentState.isEmpty || this.currentState.noMore) return;

      if (isClear) {
        this.currentState.pageNum = 1;
        this.showLoading = true;
      }

      const { currentState, listData } = this;
      this.currentState = {
        ...currentState,
        requesting: true,
        error: false,
        noMore: false,
        isEmpty: false
      };

      try {
        const { list, pageNum = 0, pages = 0 } = await this.$API.subjectListContents(
          {
            topicId: currentState.id,
            pageNum: this.currentState.pageNum,
            pageSize: COUNT
          },
          { showErrorMsg: false }
        );

        const allList = isClear === true ? list : [...listData, ...list];
        const isEmpty = allList.length === 0;

        // 通过后端返回的页码判断是否还有下一页
        const noMore = ((list && list.length) < COUNT) && (pages<= pageNum);
        this.listData = allList;
        this.currentState = {
          ...this.currentState,
          isEmpty,
          noMore
        };
        this.currentState.pageNum++;
      } catch (e) {
        this.currentState.error = true;
      } finally {
        this.showLoading = false;
        this.currentState.requesting = false;
      }
    },

    loadMore() {
      if (this.currentState.noMore) return;
      this.getList();
    }
  }
};
</script>

<style lang="scss" scoped>
@import '~assets/styles/variable.scss';

.top {
  width: 100%;
  background-image: linear-gradient(
    180deg,
    #0c0c1c 0%,
    rgba(12, 12, 28, 0.7) 100%
  );
}

.page-content {
  width: $page-width;
  margin: 0 auto;

  .layout {
    display: flex;

    .left {
      width: $page-left-width;
    }

    .right {
      width: $page-right-width;
    }
  }
}

.topic-introduce {
  padding: 24px 16px 32px 16px;

  .title {
    font-size: 20px;
    color: #ffffff;
    letter-spacing: 0;
    line-height: 32px;
    font-weight: 500;
  }

  .detail {
    display: flex;
    align-items: center;

    .cover {
      border: 0.5px solid rgba(42, 60, 88, 0.1);
      border-radius: 8px;
      width: 64px;
      height: 64px;
    }

    .name {
      font-size: 20px;
      color: #ffffff;
      letter-spacing: 0;
      line-height: 32px;
      font-weight: 500;
    }

    .num {
      font-size: 12px;
      color: rgba(255, 255, 255, 0.7);
      letter-spacing: 0;
      line-height: 16px;
    }
  }

  .summary {
    font-size: 14px;
    color: rgba(255, 255, 255, 0.7);
    letter-spacing: 0;
    line-height: 20px;
  }
}
</style>
